from common import KEY
import datetime
import time
import random


def merge_dialog_in_and_out(rows_in, rows_out):
    """按时间倒序把聊天历史的输入和输出合并到一起。"""
    rows = rows_in + rows_out
    rows = sorted(rows, key=lambda row: row[KEY], reverse=True)  # DESC
    return rows


def translate_ns(ns):
    """把纳秒时间戳翻译成年月日时分秒和毫秒。"""
    ms = ns // 1000
    mili = ms // 1000
    s = mili // 1000
    ms_ = mili % 1000
    dt = datetime.datetime.fromtimestamp(s)
    xstr = dt.strftime('%Y-%m-%d %H:%M:%S')
    return '%s.%03d' % (xstr, ms_)


def uuid():
    xns = time.time_ns()
    xns = '%020d' % xns
    xrand = random.randint(0, 999999)
    xrand = '%06d' % xrand
    xuuid = xns + '_' + xrand
    return xuuid


if '__main__' == __name__:
    for i in range(5):
        xuuid = uuid()
        print(i, xuuid)
